import React, {Component} from 'react';
import {withRouter} from 'react-router-dom';

/*
cur int 当前tab
tabs arr tabs数据
switchTab fn (index) 把序号传出去
*/

class Tabs extends Component {
  constructor (...args) {
    super(...args);
  }
  fnClick (index) {
    this.props.switchTab(index);
    // console.log(this.props)
    if (this.props.tabs[index].path && this.props.location.pathname !== this.props.tabs[index].path) {
      this.props.history.push(this.props.tabs[index].path, {});
    }
  }
  render () {
    // console.log(this.props);
    if (!this.props.tabs) console.error('tabs is required');
    return (
      <ul className="nav nav-tabs">
        {
          this.props.tabs ? (
            this.props.tabs.map((item, index) => (
              <li
                key={index}
                className={this.props.cur === index ? 'active' : ''}
                onClick={this.fnClick.bind(this, index)}
              ><a href="javascript:;">{item.text}</a></li>
            ))
          ) : ''
        }
        {/* <li className="active"><a href="#">选项1</a></li> */}
      </ul>
    )
  }
}

export default withRouter(Tabs);
